/* 
 * Spring mixin mybatis frame code general
 * Fu(iknoweth@yahoo.com) All rights reserved 
 * 
 */
package com.pricl.api.file.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.pricl.biz.file.entity.FileInfo;
import com.pricl.biz.file.service.FileService;
import com.pricl.frame.core.E;
import com.pricl.frame.core.R;
import com.wordnik.swagger.annotations.Api;

/**
 * <p>
 * <strong>附件信息表</strong>Access Controller
 * </p>
 *
 */
@Api(value="/api/file",description="文件上传")
@RequestMapping("/api/file")   
@Controller
public class FileApiController {

	@Autowired
	FileService service;
	
	
	/**
	 * 
	 * @ClassName findByBid 
	 * @Description   
	 * @author 张毕思 
	 * @date 2017年6月3日
	 * @company 车资道科技
	 * 
	 * @param bid
	 * @return
	 */
	@RequestMapping(value="findByBid",method=RequestMethod.POST)
	public @ResponseBody R findByBid(@RequestParam("bid")long bid){
		String sbid = Long.toString(bid);
		String type = "话题内容附件";
		List<FileInfo> files= null;
		files = service.findLink(type, sbid);
		return R.ok(200, files, "查询成功");
	}
	
	/**
	 * 
	 * @ClassName upload 
	 * @Description   手机端富文本图片的上传
	 * @author 张毕思 
	 * @date 2017年6月13日
	 * @company 车资道科技
	 * 
	 * @param item
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody R upload(HttpServletRequest  request,FileInfo item, @RequestParam("file") MultipartFile file) {
		item.setOname(file.getOriginalFilename());
		item.setContentType(file.getContentType());
		item.setLength(file.getSize());
		Map<String, String> map = new HashMap<String, String>();
		try {
			FileInfo fileInfo = service.uploadToGetPath(item, file.getInputStream(), file.getSize());
			String url = fileInfo.getFileUrl();
			map.put("path",url);
		} catch (IOException e) {
			throw new E("上传文件读写出错");
		}
		
		return R.ok(200,map,"上传成功");
	}
	
	/**
	 * 
	 * @ClassName uploadFile 
	 * @Description   上传圈子帖子的图片
	 * @author 张毕思 
	 * @date 2017年6月23日
	 * @company 车资道科技
	 * 
	 * @param request
	 * @param item
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public @ResponseBody R uploadFile(HttpServletRequest  request,FileInfo item, @RequestParam("file") MultipartFile file) {
		/*
		String _ip = request.getLocalAddr();
		int port = request.getLocalPort();
		String serverPath = "http://"+_ip +":"+ String.valueOf(port);
		*/
		String path = null;
		item.setOname(file.getOriginalFilename());
		item.setContentType(file.getContentType());
		item.setLength(file.getSize());
		Map<String, String> map = new HashMap<String, String>();
		try {
			FileInfo fileInfo = service.uploadToGetPath(item, file.getInputStream(), file.getSize());
			map.put("path",  fileInfo.getFileUrl());
			map.put("fileUrl",  fileInfo.getFileUrl());
			map.put("fileId",  fileInfo.getId());
		} catch (IOException e) {
			throw new E("上传文件读写出错");
		}
		
		return R.ok(200,map,"上传成功");
	}
	
	//获取播放凭证
	@RequestMapping(value = "/getPlayAuth", method = RequestMethod.POST)
	public @ResponseBody R getPlayAuth(@RequestParam("videoid")String videoId) {
		GetVideoPlayAuthResponse response= service.getVideoPlayAuth(videoId);
		return R.ok(200,response,"success");
	}
	
}
